package com.sh.wy.complaint.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sh.util.Result;
import com.sh.wy.complaint.entity.UserComplaint;
import com.sh.wy.complaint.entity.UserComplaintParam;
import com.sh.wy.complaint.mapper.UserComplaintMapper;
import com.sh.wy.complaint.service.IUserComplaintService;
import com.sh.wy.system.entity.LiveUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.parameters.P;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 世豪学Java
 * @since 2023-12-24
 */
@RestController
@RequestMapping("/api/complaint")
@Api(tags = "投诉管理")
public class UserComplaintController {
    @Autowired
    private IUserComplaintService userComplaintService;

    @ApiOperation("投诉列表")
    @GetMapping("/list")
    public Result list(UserComplaintParam param){
        IPage<UserComplaint> page = new Page<>(param.getCurrentPage(),param.getPageSize());
        QueryWrapper<UserComplaint> queryWrapper = new QueryWrapper<>();
        //判断是否为空
        queryWrapper.lambda()
                .like(StringUtils.hasText(param.getTitle()),UserComplaint::getTitle,param.getTitle())
                .like(StringUtils.hasText(param.getContent()),UserComplaint::getContent,param.getContent());
        IPage<UserComplaint> page1 = userComplaintService.page(page, queryWrapper);

        return Result.success(page1);
    }

    @ApiOperation("我的投诉列表")
    @GetMapping("/myList")
    @PreAuthorize("hasAuthority('wy:complaint:mylist')")
    public Result myList(UserComplaintParam param){
        //获取当前登录对象，判断是物主还是业主,只有业主才能投诉
        LiveUser liveUser = getCurrentUser();
        if (Objects.isNull(liveUser)){
            return Result.error(500,"非业主登录");
        }
        IPage<UserComplaint> page = new Page<>(param.getCurrentPage(),param.getPageSize());
        QueryWrapper<UserComplaint> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .like(StringUtils.hasText(param.getTitle()),UserComplaint::getTitle,param.getTitle())
                .like(StringUtils.hasText(param.getContent()),UserComplaint::getContent,param.getContent())
                .eq(UserComplaint::getUserId,liveUser.getUserId());
        IPage<UserComplaint> page1 = userComplaintService.page(page, queryWrapper);
        return Result.success(page1);
    }

    private LiveUser getCurrentUser() {
        //从本地线程获取登录对象
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        //如果能强转为LiveUser则返回，否则返回null
        if (principal instanceof LiveUser){
            return (LiveUser) principal;
        }
        return null;
    }

    @ApiOperation("添加投诉")
    @PostMapping
    public Result addComplaint(@RequestBody UserComplaint userComplaint){
        userComplaint.setUserId(getCurrentUser().getUserId());
        if (userComplaintService.save(userComplaint)) {
            return Result.success();
        }
        return Result.error();
    }

    @ApiOperation("修改投诉")
    @PutMapping
    public Result editComplaint(@RequestBody UserComplaint userComplaint){
        if (userComplaintService.updateById(userComplaint)) {
            return Result.success();
        }
        return Result.error();
    }

    @ApiOperation("删除投诉")
    @DeleteMapping("/{complaintId}")
    public Result deleteComplaint(@PathVariable("complaintId") Integer complaintId){
        if (userComplaintService.removeById(complaintId)) {
            return Result.success();
        }
        return Result.error();
    }
}
